<?php

namespace Summer\Shop\Http\Controllers;

use Illuminate\Routing\Controller;
use RLuders\JWTAuth\Classes\JWTAuth;
use Summer\Hub\Http\Controllers\Traits\ApiReturn;
use Input;
use Exception;
use Db;
use Summer\Shop\Models\CommentModel;
use Summer\Shop\Models\OrderItemModel;
use Summer\Shop\Models\OrderModel;
use System\Models\File as FileModel;


class CommentController extends Controller
{
    use ApiReturn;
    /**
     * 商品列表
     */
    public function index()
    {
        $product_id=Input::get('goods_id');
        $page=Input::get('page',1);
        $page_size=Input::get('page_size',20);
        $query=CommentModel::select();
        $query->with([
            'album',
            'target'=>function($q){
                $q->with(['avatar']);
            }
        ]);
        $query->where('product_id',$product_id);
        $query->where('is_show',1);
        $records=$query->paginate($page_size,$page);
        $records->each(function ($record){
            $images=[];
            foreach ($record->album as $album){
                $images[]=$album->path;
            }
            $record->images=$images;
        });
        return $this->apiSuccess('操作成功',$records);
    }

    /**
     * 评价订单
     */
    public function create(JWTAuth $auth){
        if (!$user = $auth->user()) {
            return $this->apiError('登录失效,请重新登录');
        }
        $order_id=Input::get('id');
        if(!$order_id){
            return $this->apiError('参数错误');
        }
        $order_item_id=Input::get('order_item_id');
        if(!$order_item_id){
            return $this->apiError('参数错误');
        }
        $order_item=OrderItemModel::where('user_id',$user->id)->where('id',$order_item_id)->where('order_id',$order_id)->first();
        if(!$order_item){
            return $this->apiError('订单商品信息获取失败');
        }
        if($order_item->status_slug!='nocomment'){
            return $this->apiError('该订单商品不能评价了');
        }
        $level=Input::get('level');
        $content=Input::get('content');
        $is_show_dfault=0;//默认是否显示,后期根据配置设置
        Db::beginTransaction();
        try {
            $comment=new CommentModel();
            $comment->user_id=$user->id;
            $comment->product_id=$order_item->product_id;
            $comment->order_id=$order_item->order_id;
            $comment->order_item_id=$order_item->id;
            $comment->order_item_name=$order_item->name;
            $comment->order_item_description=$order_item->description;
            $comment->level=$level;
            $comment->content=$content;
            $comment->is_show=$is_show_dfault;
            $comment->is_anonymous=0;
            $comment->save();
            $album_ids=Input::get('album_ids');
            foreach ($album_ids as $album_id){
                $file=FileModel::where('id',$album_id)->first();
                $comment->album()->add($file);
            }
            $order_item->status_slug='commented';
            $order_item->save();
            //判断该订单的商品是否都评价过
            $nocomment_count=OrderItemModel::where('order_id',$order_id)->where('status_slug','nocomment')->count();
            if($nocomment_count==0){
                OrderModel::where('id',$order_id)->where('status_slug','nocomment')->update(['status_slug'=>'finish','status'=>2]);
            }
            Db::commit();
        }catch (Exception $e){
            Db::rollBack();
            return $this->apiError($e->getMessage());
        }
        return $this->apiSuccess('操作成功');
    }


}
